package stack;

import java.util.Arrays;

public class MyStack {

    private int[] elem;
    private int usedSize;

    private static final int DEFAULT_CAPACITY = 10;

    public MyStack(){
        this.elem = new int[DEFAULT_CAPACITY];
    }

    //将元素压入栈
    public void push(int val){
        if(isFull()){
            elem = Arrays.copyOf(elem,elem.length * 2);
        }
        elem[usedSize] = val;
        usedSize++;
    }

    //判满
    private boolean isFull(){
        return usedSize == elem.length;
    }

    //弹出栈顶元素
    public void pop(){
        if(isEmpty()){
            throw new EmptyException("栈为空");
        }
        usedSize--;
    }

    //判空
    public boolean isEmpty(){
        return usedSize == 0;
    }

    //查看栈顶元素
    public int peek(){
        if(isEmpty()){
            throw new EmptyException("栈为空");
        }
        return elem[usedSize - 1];
    }

}
